package com.bookmanager.www.controller;


import cn.hutool.core.map.MapUtil;
import com.bookmanager.www.common.utils.Result;
import com.bookmanager.www.entity.FlBook;
import com.bookmanager.www.entity.FlCategories;
import com.bookmanager.www.entity.FlClassify;
import com.bookmanager.www.mapper.FlBookMapper;
import com.bookmanager.www.mapper.FlCategoriesMapper;
import com.bookmanager.www.service.FlCategoriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 图书的类别 前端控制器
 * </p>
 *
 * @author 风离
 * @since 2021-09-17
 */
@RestController
@RequestMapping("/fl-categories")
@Api(tags = "分类管理")
public class FlCategoriesController {
    @Resource
    FlCategoriesMapper flCategoriesMapper;
    @Resource
    FlCategoriesService flCategoriesService;
    @Resource
    FlBookMapper flBookMapper;


    @GetMapping("/byCategory")
    @ApiOperation(value = "分类排行榜",notes = "根据豆瓣评分进行排行 取top10")
    Result byCategory(@RequestParam("categoriesId")Integer categoriesId,@RequestParam("size")Integer size)
    {
        List<FlBook> bookList = flBookMapper.getCategoriesBooksOrderByDoubanScoreDesc(categoriesId);
        List bookList3=new LinkedList();
        if(bookList.size()!=0)
        {
            //排行数量
            if(bookList.size()<size||size<0)
            {
                return Result.fail("Size超过该分类下最大图书数量！！或者Size<0!!!","");
            }
            else {
                List<FlBook> bookList2 =new ArrayList<>();
                for(int i=0;i<size;i++)
                {
                    bookList2.add(bookList.get(i));
                }

                //前端需要一个categoryName
                bookList3.add(flCategoriesMapper.selectCategoriesNameByCategoriesId(categoriesId));
                bookList3.add(bookList2);
                return Result.succ("获取分类排行榜成功！！",bookList3);
            }
        }
        return Result.fail("获取分类排行榜信息失败！！","");
    }

    @GetMapping("getAllCategories")
    @ApiOperation(value = "获取所有分类信息",notes = "直接访问即可 无需传参")
    Result getAllCategories(){
        List<FlCategories> flCategoriesList = flCategoriesMapper.getAllCategories();
        if (flCategoriesList!=null)
        {
            return Result.succ("获取分类信息成功！",flCategoriesList);
        }
        return Result.fail("获取分类信息失败","");
    }
    @GetMapping("getAllClassify")
    @ApiOperation(value = "获取所有大分类",notes = "无需传参")
    Result getAllClassify()
    {
        List<Object> arrList=new ArrayList<>();

        List<FlClassify> flClassifies = flCategoriesMapper.getAllClassify();
        if (flClassifies!=null)
        {
            for (int i = 0; i <flClassifies.size() ; i++) {
                Map hashMap=new LinkedHashMap();
                hashMap.put("categoriesId",flClassifies.get(i).getClassifyId());
                hashMap.put("classname",flClassifies.get(i).getClassifyName());
                hashMap.put("flag",1);
                List<FlCategories> flCategoriesList = flCategoriesMapper.getCategoriesByClassifyId(flClassifies.get(i).getClassifyId());
                hashMap.put("CategoriesList",flCategoriesList);
                arrList.add(hashMap);
            }
            return Result.succ("获取大分类信息成功！",arrList);
        }

        return Result.fail("获取大分类信息失败","");
    }

    @PostMapping("addCategories")
    @ApiOperation(value = "添加一个分类信息",notes = "传入分类名 该分类展示的图片 该分类所属大分类")
    @RequiresRoles(value = {"admin","superadmin"},logical = Logical.OR)
    Result addCategories(@RequestParam("categoriesName")String categoriesName,
    @RequestParam("topImage")String topImage,@RequestParam("classifyId")Integer classifyId
    )
    {
        FlCategories existing = flCategoriesMapper.selectCategoriesByCategoriesName(categoriesName);
        if(existing!=null)
        {
            return Result.fail("添加到数据库失败","失败原因可能是该分类名已存在");
        }
        int insertCategories = flCategoriesMapper.addOneCategories(categoriesName,topImage,classifyId);
        if (insertCategories!=0)
        {
            return Result.succ("添加一个分类成功","");
        }
        return Result.fail("添加到数据库失败","失败原因未知");
    }

    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @PostMapping("updateCategories")
    @ApiOperation(value = "修改分类信息",notes = "传入旧分类名和新分类名")
    Result updateCategories(@RequestParam("oldCategoriesName")String oldCategoriesName,@RequestParam("newCategoriesName")String newCategoriesName)
    {
        int updated = flCategoriesMapper.updateCategoriesByOldCategoriesName(oldCategoriesName,newCategoriesName);
        if (updated!=0)
        {
            return Result.succ("修改分类名成功",MapUtil.builder().put("newCategoriesName",newCategoriesName).build());
        }

        return Result.fail("修改分类名失败！","");
    }

    @DeleteMapping("deleteCategories")
    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "删除某个分类",notes = "传入分类名即可 注`此操作会删除该分类及该分类下所有图书信息！！`")
    Result deleteCategories(@RequestParam("categoriesName")String categoriesName)
    {
      int deleted =  flCategoriesService.deleteCategoriesByCategoriesName(categoriesName);
      if (deleted!=0)
      {
          return Result.succ("删除该分类成功 包括及该分类下所有图书！！！","");
      }

        return Result.fail("删除分类失败","");
    }



}
